package cn.meomis.action;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.meomis.model.User;
import cn.meomis.service.UserService;

public class UserAction extends BaseAction {

	@Autowired
	private UserService userService;
	
	@Action(value = "/userAction", results = { @Result(name = SUCCESS, location = "/manager/modules/user/index.jsp") })
	@Override
	public String execute(){
		List<User> userList = userService.findAll();
		httpServletRequest.setAttribute("USER_LIST", userList);
		return SUCCESS;
	}
	
	@Action(value = "/userFindAction", results = { @Result(name = SUCCESS, location = "/manager/modules/user/edit.jsp") })
	public String find(){
		User user = userService.findById(Integer.parseInt(httpServletRequest.getParameter("id")));
		httpServletRequest.setAttribute("USER", user);
		return SUCCESS;
	}
	
/*	@Action(value = "/userAddAction", results = { @Result(name = SUCCESS, location = "/userAction.action", type = "redirect") })
	public String add() throws UnsupportedEncodingException{
		User user = new User();
		user.setUserid(new String(httpServletRequest.getParameter("userid").getBytes("ISO-8859-1"),"utf-8"));
		user.setUserrealname(new String(httpServletRequest.getParameter("userrealname").getBytes("ISO-8859-1"),"utf-8"));
		user.setTel(new String(httpServletRequest.getParameter("tel").getBytes("ISO-8859-1"),"utf-8"));
		user.setEmail(new String(httpServletRequest.getParameter("email").getBytes("ISO-8859-1"),"utf-8"));
		user.setDepartment(new String(httpServletRequest.getParameter("department").getBytes("ISO-8859-1"),"utf-8"));
		user.setState(new String(httpServletRequest.getParameter("state").getBytes("ISO-8859-1"),"utf-8"));
		user.setRemark(new String(httpServletRequest.getParameter("remark").getBytes("ISO-8859-1"),"utf-8"));
		user.setPasswd(new String(httpServletRequest.getParameter("password").getBytes("ISO-8859-1"),"utf-8"));
//		user.setUsername(new String(httpServletRequest.getParameter("username").getBytes("ISO-8859-1"),"utf-8"));
//		user.setPassword(new String(httpServletRequest.getParameter("password").getBytes("ISO-8859-1"),"utf-8"));
//		user.setAge(Integer.parseInt(httpServletRequest.getParameter("age")));
		userService.save(user);
		return SUCCESS;
	}*/
	
	
	
	
	
	@Action(value = "/userEditAction", results = { @Result(name = SUCCESS, location = "/userAction.action", type = "redirect") })
	public String edit(){
		User user = new User();
		user.setId(Integer.parseInt(httpServletRequest.getParameter("id")));
//		user.setUsername(httpServletRequest.getParameter("username"));
//		user.setPassword(httpServletRequest.getParameter("password"));
//		user.setAge(Integer.parseInt(httpServletRequest.getParameter("age")));
		userService.save(user);
		return SUCCESS;
	}
	
	@Action(value = "/userDeleteAction", results = { @Result(name = SUCCESS, location = "/userAction.action", type = "redirect") })
	public String delete(){
		userService.removeById(Integer.parseInt(httpServletRequest.getParameter("id")));
		return SUCCESS;
	}
	
	@Action(value = "/addUserAction", results = { @Result(name = SUCCESS, location = "/manager/modules/user/save.jsp") })
	public String addUser(){
//		User user = userService.findById(Integer.parseInt(httpServletRequest.getParameter("id")));
//		httpServletRequest.setAttribute("USER", user);
		return SUCCESS;
	}
}
